package cc.chinagps.gboss.mq;

import cc.chinagps.gboss.PackageScanner;
import cc.chinagps.gboss.common.Config;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Session;

public class MQManager {
    private ConnectionFactory factory;

    private MQManager() {
    }

    private static class SingletonHolder {
        private static final MQManager INSTANCE = new MQManager();
    }

    public static final MQManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void init() throws Exception {
        factory = new ActiveMQConnectionFactory(Config.MQ.URL);
        Connection readerConn = openConnection();
        readerConn.start();
        Session readSession = readerConn.createSession(true, Session.AUTO_ACKNOWLEDGE);
        PackageScanner.getInstance().initMqReader(readSession);
    }

    public Connection openConnection() throws Exception {
        return factory.createConnection();
    }

}
